package com.jy.adscm.operation.aftersales.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.aftersales.bean.ShRepairMaterialList;
import com.jy.adscm.operation.aftersales.bean.ShRepairMaterialListCheck;
import com.jy.adscm.operation.aftersales.bean.ShRepairUpkeepMaterialList;
import com.jy.adscm.operation.aftersales.bean.ShRepairUpkeepMaterialListCheck;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShRepairUpkeepMaterialListCheckMapper;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShRepairUpkeepMaterialListMapper;
import com.jy.adscm.operation.aftersales.service.ShRepairUpkeepMaterialListCheckService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-08-29
 */
@Service
public class ShRepairUpkeepMaterialListCheckServiceImpl extends ServiceImpl<ShRepairUpkeepMaterialListCheckMapper, ShRepairUpkeepMaterialListCheck> implements ShRepairUpkeepMaterialListCheckService {
     @Autowired
    JyUserMapper jyUserMapper;
     @Autowired
    ShRepairUpkeepMaterialListMapper shRepairUpkeepMaterialListMapper;
    @Override
    public boolean updateshRepairUpkeepMaterialListCheck(List<ShRepairUpkeepMaterialListCheck> shRepairUpkeepMaterialListChecks, Token token) {

        for (ShRepairUpkeepMaterialListCheck materialListCheck : shRepairUpkeepMaterialListChecks) {
            if ("结果".equals(materialListCheck.getName())){
                //查询到检查的物料
                ShRepairUpkeepMaterialList shRepairUpkeepMaterialList = shRepairUpkeepMaterialListMapper.selectById(materialListCheck.getPid());
                if ("1".equals(materialListCheck.getValue())){
                    shRepairUpkeepMaterialList.setState("1");
                }
                if ("0".equals(materialListCheck.getValue())){
                    shRepairUpkeepMaterialList.setState("0");
                }
                shRepairUpkeepMaterialList.setCreator(token.getUser().getId().toString());
                shRepairUpkeepMaterialList.setCreateTime(new Date());
                //修改这个物料
                shRepairUpkeepMaterialListMapper.updateById(shRepairUpkeepMaterialList);
            }
            boolean b = super.updateById(materialListCheck);
        }
        return true;
    }

    @Override
    public List<ShRepairUpkeepMaterialListCheck> selectByPid(String pid) {
        Wrapper<ShRepairUpkeepMaterialListCheck> wrapper=new EntityWrapper<>();
        wrapper.eq("pid",pid);
        wrapper.orderBy("rank",false);
        List<ShRepairUpkeepMaterialListCheck> shRepairMaterialListChecks = super.selectList(wrapper);
        for (ShRepairUpkeepMaterialListCheck shRepairUpkeepMaterialListCheck : shRepairMaterialListChecks) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(shRepairUpkeepMaterialListCheck.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(shRepairUpkeepMaterialListCheck.getCreator());
                shRepairUpkeepMaterialListCheck.setCreatorName(jyUser.getName());
            }
        }
        return shRepairMaterialListChecks;
    }
}
